package com.qf.service.impl.blog;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.qf.api.blog.entity.TagEntity;
import com.qf.api.blog.service.TagService;
import com.qf.core.exception.BaseException;
import com.qf.core.response.ResponseCode;
import com.qf.dao.blog.mapper.TagMapper;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.util.Objects;

/**
* @author sin
* @description 针对表【t_tag(文章标签表)】的数据库操作Service实现
* @createDate 2025-05-23 14:19:29
*/
@Service
@Slf4j
public class TagServiceImpl extends ServiceImpl<TagMapper, TagEntity>
    implements TagService {

    @Resource
    private TagMapper tagMapper;

    @Override
    public Boolean addTag(String tagName) {
        // 先判断该标签是否已经存在
        TagEntity tagEntity = tagMapper.selectOne(new QueryWrapper<TagEntity>().eq("name",tagName));
        if (Objects.nonNull(tagEntity)) {
            log.warn("标签名称： {}, 此标签已存在", tagName);
            throw new BaseException(ResponseCode.TAG_NAME_IS_EXISTED);
        }
        TagEntity build = TagEntity.builder()
                .name(tagName.trim())
                .build();
        return tagMapper.insert(build) > 0;
    }

    @Override
    public Boolean updateTag(Long id, String tagName) {
        // 先判断该标签是否已经存在
        TagEntity tagEntity = tagMapper.selectOne(new QueryWrapper<TagEntity>().eq("name",tagName));
        if (Objects.nonNull(tagEntity)) {
            log.warn("标签名称： {}, 此标签已存在", tagName);
            throw new BaseException(ResponseCode.TAG_NAME_IS_EXISTED);
        }
        TagEntity updateTagDO = TagEntity.builder().id(id).name(tagName.trim()).build();
        return tagMapper.updateById(updateTagDO) > 0;
    }
}




